1366B - Shuffle - CodeForces Solution


math two pointers *1300

Please click on ads to support us..

Python Code:

import heapq
from collections import *
import math
def solve():

    n, x, m = map(int, input().split())
    maxi, mini = float("-inf"), float("inf")
    x1, x2 = x, x
    for i in range(m):
        a, b = map(int, input().split())
        f = 0
        if (x1 >= a and x1 <= b) or (x2 >=a and x2<=b):
            f = 1
            maxi = max(maxi, a, b)
            mini = min(mini, a, b)
        if f == 1:
            x1 = mini
            x2 = maxi
    if maxi - mini + 1 == float("-inf"):
        print(1)
        return 
    print(maxi - mini + 1)


t = int(input())
for _ in range(t):
    solve()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mod 1000000007
#define ff first
#define se second
#define vc vector
ll bpow(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1)
        {
            res=res*a;
            res%=mod;
        }
        a=a*a;
        a%=mod;
        b=b>>1;
    }
    return res;
}
ll fact(ll p)
{
	ll ans=1;
	for(ll i=1;i<=p;i++)
	{
		ans*=i;
		ans%=mod;
	}
	return ans;
}
string num(ll n)
{
	string res="";
	while(n)
	{
		ll po=n%10;
		n=n/10;
		res+=to_string(po);
	}
	reverse(res.begin(),res.end());
	return res;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		 ll n,i,j,x,m;
	    cin>>n>>x>>m;
	    vector<pair<ll,ll>>v1(m);
	    for(i=0;i<m;i++)
	    {
	        ll l1,r1;
	        cin>>l1>>r1;
	        v1[i]={l1,r1};
	    }
	    ll curl=0,curr=0,op;
	    for(i=0;i<m;i++)
	    {
	        ll l2,r2;
	        l2=v1[i].ff;
	        r2=v1[i].se;
	        if(l2<=x and x<=r2)
	        {
	            curl=l2;
	            curr=r2;
	            op=i;
	            break;
	        }
	    }
	    if(curl!=0 and curr!=0)
	    {
	        for(i=op+1;i<m;i++)
	        {
	            ll l2,r2;
	        l2=v1[i].ff;
	        r2=v1[i].se;
	        if(l2>curr or r2<curl)
	        continue;
	        curl=min(curl,l2);
	        curr=max(curr,r2);
	        }
	    }
	    ll ans;
	    if(curl==0 and curr==0)
	    ans=1;
	    else 
	    ans=curr-curl+1;
	    cout<<ans<<"\n";
	}
}


Comments

Submit
0 Comments
More Questions

1388A - Captain Flint and Crew Recruitment
592B - The Monster and the Squirrel
1081A - Definite Game
721C - Journey
1400A - String Similarity
1734E - Rectangular Congruence
1312D - Count the Arrays
424C - Magic Formulas
1730C - Minimum Notation
1730B - Meeting on the Line
1730A - Planets
302B - Eugeny and Play List
1730D - Prefixes and Suffixes
1515C - Phoenix and Towers
998A - Balloons
1734F - Zeros and Ones
1144B - Parity Alternated Deletions
92B - Binary Number
1144C - Two Shuffled Sequences
1154B - Make Them Equal
1272B - Snow Walking Robot
522B - Photo to Remember
608B - Hamming Distance Sum
1408F - Two Different
274B - Zero Tree
1726H - Mainak and the Bleeding Polygon
722A - Broken Clock
129B - Students and Shoelaces
697B - Barnicle
903D - Almost Difference